package com.ikas.ai.server.module.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.module.system.dao.LogDAO;
import com.ikas.ai.server.module.system.model.Log;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogService extends ServiceImpl<LogDAO, Log> {

    @Autowired
    private LogDAO logDAO;

    public List<Log> getLogs(String userName, String type, String startTime, String endTime) {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userName)) {
            queryWrapper.lambda().like(Log::getUserName, userName);
        }
        if (StringUtils.isNotBlank(type)) {
            queryWrapper.lambda().eq(Log::getType, type);
        }
        if (StringUtils.isNotBlank(startTime)) {
            queryWrapper.lambda().gt(Log::getCreateTime, startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            queryWrapper.lambda().lt(Log::getCreateTime, endTime);
        }
        return logDAO.selectList(queryWrapper);
    }

    public void addLog(Log log) {
        logDAO.insert(log);
    }
}
